package cn.core.coding.service.impl;

import cn.core.coding.mapper.CodingMapper;
import cn.core.coding.service.intf.IGetDbInfoService;
import cn.core.coding.vo.DbTablesInfoVO;
import cn.core.coding.vo.MysqlDbTableColInfoVO;
import cn.core.coding.vo.MysqlDbTableInfoVO;
import cn.core.coding.vo.MysqlDbTablePkInfoVO;
import cn.core.coding.vo.OracleDbTableColInfoVO;
import cn.core.coding.vo.OracleDbTableInfoVO;
import cn.core.coding.vo.OracleDbTablePkInfoVO;
import cn.core.tool.exception.CoreException;
import cn.core.tool.util.CoreTool;
import com.baomidou.dynamic.datasource.annotation.DS;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

/**
 * 获取数据库信息 服务实现
 */
@Log4j2
@Primary
@Service
public class GetDbInfoServiceImpl implements IGetDbInfoService {

  @Resource
  private CodingMapper codingMapper;

  @Override
  @DS("#dsName")
  public Collection<DbTablesInfoVO> getMysqlDbAllTablesInfoByDbName(String dsName, String dbName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(dbName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      return codingMapper.getMysqlDbAllTablesInfoByDbName(dbName);
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public Collection<DbTablesInfoVO> getOracleDbAllTablesInfo(String dsName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      return codingMapper.getOracleDbAllTablesInfo();
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public MysqlDbTableInfoVO getMysqlDbTableInfo(String dsName, String dbName, String tableName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(dbName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(tableName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      Collection<MysqlDbTableInfoVO> mysqlDbTableInfoVOSet = codingMapper.getMysqlDbTableInfoByDbNameAndTableName(dbName, tableName);
      if (CoreTool.isNotEmpty(mysqlDbTableInfoVOSet)) {
        return mysqlDbTableInfoVOSet.iterator().next();
      } else {
        throw new CoreException("数据库表不存在！请检查配置是否正确！");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public Collection<MysqlDbTableColInfoVO> getMysqlDbTableColInfo(String dsName, String dbName, String tableName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(dbName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(tableName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      Collection<MysqlDbTableColInfoVO> mysqlDbTableColInfoVOSet = codingMapper.getMysqlDbTableColInfoByDbNameAndTableName(dbName, tableName);
      if (CoreTool.isNotEmpty(mysqlDbTableColInfoVOSet)) {
        return mysqlDbTableColInfoVOSet;
      } else {
        throw new CoreException("数据库表列不存在！请检查配置是否正确！");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public MysqlDbTablePkInfoVO getMysqlDbTablePkInfo(String dsName, String dbName, String tableName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(dbName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(tableName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      Collection<MysqlDbTablePkInfoVO> mysqlDbTablePkInfoVOSet = codingMapper.getMysqlDbTablePkInfoByDbNameAndTableName(dbName, tableName);
      if (CoreTool.isNotEmpty(mysqlDbTablePkInfoVOSet)) {
        return mysqlDbTablePkInfoVOSet.iterator().next();
      } else {
        throw new CoreException("数据库表主键不存在！请检查配置是否正确！");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public OracleDbTableInfoVO getOracleDbTableInfo(String dsName, String tableName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(tableName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      Collection<OracleDbTableInfoVO> oracleDbTableInfoVOSet = codingMapper.getOracleDbTableInfoByTableName(tableName);
      if (CoreTool.isNotEmpty(oracleDbTableInfoVOSet)) {
        return oracleDbTableInfoVOSet.iterator().next();
      } else {
        throw new CoreException("数据库表不存在！请检查配置是否正确！");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public Collection<OracleDbTableColInfoVO> getOracleDbTableColInfo(String dsName, String tableName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(tableName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      Collection<OracleDbTableColInfoVO> oracleDbTableColInfoVOSet = codingMapper.getOracleDbTableColInfoByTableName(tableName);
      if (CoreTool.isNotEmpty(oracleDbTableColInfoVOSet)) {
        return oracleDbTableColInfoVOSet;
      } else {
        throw new CoreException("数据库表列不存在！请检查配置是否正确！");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public String getOracleDbTablePrimaryKeyConstraintName(String dsName, String tableName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(tableName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      Collection<String> primaryKeyConstraintNameSet = codingMapper.getOracleDbTablePrimaryKeyConstraintNameByTableName(tableName);
      if (CoreTool.isNotEmpty(primaryKeyConstraintNameSet)) {
        return primaryKeyConstraintNameSet.iterator().next();
      } else {
        throw new CoreException("数据库表主键约束名称不存在！请检查配置是否正确！");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

  @Override
  @DS("#dsName")
  public OracleDbTablePkInfoVO getOracleDbTablePkInfo(String dsName, String tableName, String primaryKeyConstraintName) throws Exception {
    try {
      if (CoreTool.isNullStr(dsName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(tableName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      if (CoreTool.isNullStr(primaryKeyConstraintName)) {
        throw new CoreException(CoreException.E_PARAM_ERR);
      }
      Collection<OracleDbTablePkInfoVO> oracleDbTablePkInfoVOSet = codingMapper.getOracleDbTablePkInfoByTableNameAndPrimaryKeyConstraintName(tableName, primaryKeyConstraintName);
      if (CoreTool.isNotEmpty(oracleDbTablePkInfoVOSet)) {
        return oracleDbTablePkInfoVOSet.iterator().next();
      } else {
        throw new CoreException("数据库表主键不存在！请检查配置是否正确！");
      }
    } catch (Exception e) {
      if (log.isDebugEnabled()) {
        log.error(e.getMessage(), e);
      }
      throw e;
    }
  }

}
